package org.apache.ibatis.test.testJdbc;


import com.mysql.cj.jdbc.Driver;

import java.sql.*;

/**
 * @Description
 * @Author houchenkai
 * @Created Date: 2020/9/21 18:33
 * @ClassName
 */
public class YuanShengJdbc {

  private final static String url ="jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC";
  private final static String username ="root";
  private final static String password ="root";

  static {
    try {
      Class.forName(Driver.class.getName());
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static Connection getConnectDb(){
    Connection conn = null;
    try{
      conn = DriverManager.getConnection(url,username,password);

    } catch (SQLException e){
      e.printStackTrace();
    }
    return conn;
  }

  public static void closeDB(ResultSet rs, PreparedStatement stm, Connection conn){
    if(rs!=null)
    {
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    if(stm!=null)
    {
      try {
        stm.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    if(conn!=null)
    {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }



  public static void main(String[] args) throws Exception {
    Connection connection = getConnectDb();
    DatabaseMetaData metaData = connection.getMetaData();
    String userName = metaData.getUserName();
    System.out.println(userName);
    PreparedStatement preparedStatement = connection.prepareStatement(" select * from payment where id =?");
    ParameterMetaData parameterMetaData = preparedStatement.getParameterMetaData();
    System.out.println(parameterMetaData);
    preparedStatement.setString(1,"1");
    ResultSet resultSet = preparedStatement.executeQuery();


    while (resultSet.next()) {
      String string = resultSet.getString(1);
      System.out.println(1+"-----------------"+string);
      ResultSetMetaData metaData1 = resultSet.getMetaData();
      String columnName1 = resultSet.getMetaData().getColumnName(1);
      int columnCount = metaData1.getColumnCount();
      String columnLabel = metaData1.getColumnLabel(1);
      String columnName = metaData1.getColumnName(1);
      String columnClassName = metaData1.getColumnClassName(1);

      String userName1 = metaData1.getColumnTypeName(1);
      int columnType = metaData1.getColumnType(1);
      System.out.println(columnCount);
      System.out.println(columnLabel);
      System.out.println(columnName);
      System.out.println(columnClassName);
      System.out.println(userName1);
      System.out.println(columnType);
      String columnName2 = resultSet.getMetaData().getColumnName(2);
      System.out.println(columnName1+":"+resultSet.getString(1));
      System.out.println(columnName2+":"+resultSet.getString(2));

    }
    closeDB(resultSet,preparedStatement,connection);
  }

}
